package com.ciko.code.views;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.mywork.R;

/**
 * Created by 12154 on 2015/10/29.
 */
public class FunctionBarView extends FrameLayout implements View.OnClickListener{

    private View contentView;

    private TextView tvStudyFuncationBar;
    private TextView tvResFuncationBar;
    private TextView tvWorkFuncationBar;
    private TextView tvPublicityFuncationBar;
    private TextView tvMsgFuncationBar;

    private ImageView ivStudyFuncationBar;
    private ImageView ivResFuncationBar;
    private ImageView ivWorkFuncationBar;
    private ImageView ivPublicityFuncationBar;
    private ImageView ivMsgFuncationBar;

    private LinearLayout rectFunctionBarStudy;
    private LinearLayout rectFuncationBarRes;
    private LinearLayout rectFuncationBarWork;
    private LinearLayout rectFuncationBarPublicity;
    private LinearLayout rectFuncationBarMsg;

    private OnFuncationSelectListener onFuncationSelectListener;

    public FunctionBarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initLayout();
        initView();
        initListener();

        tvStudyFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
        ivStudyFuncationBar.setImageResource(R.drawable.icon_home_study_pressed);

    }

    private void initLayout() {
        contentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_function_bar,null);
        addView(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }

    private void initView() {
        tvStudyFuncationBar = (TextView) contentView.findViewById(R.id.tv_study_funcation_bar);
        tvResFuncationBar = (TextView) contentView.findViewById(R.id.tv_res_funcation_bar);
        tvWorkFuncationBar = (TextView) contentView.findViewById(R.id.tv_work_funcation_bar);
        tvPublicityFuncationBar = (TextView) contentView.findViewById(R.id.tv_publicity_funcation_bar);
        tvMsgFuncationBar = (TextView) contentView.findViewById(R.id.tv_msg_funcation_bar);

        ivStudyFuncationBar = (ImageView) contentView.findViewById(R.id.iv_study_funcation_bar);
        ivResFuncationBar = (ImageView) contentView.findViewById(R.id.iv_res_funcation_bar);
        ivWorkFuncationBar = (ImageView) contentView.findViewById(R.id.iv_work_funcation_bar);
        ivPublicityFuncationBar = (ImageView) contentView.findViewById(R.id.iv_publicity_funcation_bar);
        ivMsgFuncationBar = (ImageView) contentView.findViewById(R.id.iv_msg_funcation_bar);

        rectFunctionBarStudy = (LinearLayout) contentView.findViewById(R.id.rect_function_bar_study);
        rectFuncationBarRes = (LinearLayout) contentView.findViewById(R.id.rect_funcation_bar_res);
        rectFuncationBarWork = (LinearLayout) contentView.findViewById(R.id.rect_funcation_bar_work);
        rectFuncationBarPublicity = (LinearLayout) contentView.findViewById(R.id.rect_funcation_bar_publicity);
        rectFuncationBarMsg = (LinearLayout) contentView.findViewById(R.id.rect_funcation_bar_msg);
    }

    private void initListener() {
        rectFunctionBarStudy.setOnClickListener(this);
        rectFuncationBarRes.setOnClickListener(this);
        rectFuncationBarWork.setOnClickListener(this);
        rectFuncationBarPublicity.setOnClickListener(this);
        rectFuncationBarMsg.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {

        restartAll();
        if(v==rectFunctionBarStudy){
            tvStudyFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
            ivStudyFuncationBar.setImageResource(R.drawable.icon_home_study_pressed);

            onFuncationSelectListener.OnFuncationSelect(0);
        } else if(v== rectFuncationBarRes){
            tvResFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
            ivResFuncationBar.setImageResource(R.drawable.icon_home_res_pressed);

            onFuncationSelectListener.OnFuncationSelect(1);
        } else if(v==rectFuncationBarWork) {
            tvWorkFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
            ivWorkFuncationBar.setImageResource(R.drawable.icon_home_work_press);

            onFuncationSelectListener.OnFuncationSelect(2);
        } else if(v== rectFuncationBarPublicity){
            tvPublicityFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
            ivPublicityFuncationBar.setImageResource(R.drawable.icon_home_publicity_pressed);

            onFuncationSelectListener.OnFuncationSelect(3);
        } else if(v==rectFuncationBarMsg){
            tvMsgFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
            ivMsgFuncationBar.setImageResource(R.drawable.icon_home_msg_pressed);

            onFuncationSelectListener.OnFuncationSelect(4);
        }

//        switch (v.getId()){
//            case R.id.rect_function_bar_study:
//                tvStudyFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
//                ivStudyFuncationBar.setImageResource(R.drawable.icon_home_study_pressed);
//
//                onFuncationSelectListener.OnFuncationSelect(0);
//                break;
//            case R.id.rect_funcation_bar_res:
//                tvResFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
//                ivResFuncationBar.setImageResource(R.drawable.icon_home_res_pressed);
//
//                onFuncationSelectListener.OnFuncationSelect(1);
//                break;
//            case R.id.rect_funcation_bar_work:
//                tvWorkFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
//                ivWorkFuncationBar.setImageResource(R.drawable.icon_home_work_press);
//
//                onFuncationSelectListener.OnFuncationSelect(2);
//                break;
//            case R.id.rect_funcation_bar_publicity:
//                tvPublicityFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
//                ivPublicityFuncationBar.setImageResource(R.drawable.icon_home_publicity_pressed);
//
//                onFuncationSelectListener.OnFuncationSelect(3);
//                break;
//            case R.id.rect_funcation_bar_msg:
//                tvMsgFuncationBar.setTextColor(getResources().getColor(R.color.nav_bg_root));
//                ivMsgFuncationBar.setImageResource(R.drawable.icon_home_msg_pressed);
//
//                onFuncationSelectListener.OnFuncationSelect(4);
//                break;
//        }
    }

    private void restartAll(){
        tvStudyFuncationBar.setTextColor(getResources().getColor(R.color.text_black_5));
        tvResFuncationBar.setTextColor(getResources().getColor(R.color.text_black_5));
        tvWorkFuncationBar.setTextColor(getResources().getColor(R.color.text_black_5));
        tvPublicityFuncationBar.setTextColor(getResources().getColor(R.color.text_black_5));
        tvMsgFuncationBar.setTextColor(getResources().getColor(R.color.text_black_5));

        ivStudyFuncationBar.setImageResource(R.drawable.icon_home_study_normal);
        ivResFuncationBar.setImageResource(R.drawable.icon_home_res_normal);
        ivWorkFuncationBar.setImageResource(R.drawable.icon_home_work_normal);
        ivPublicityFuncationBar.setImageResource(R.drawable.icon_home_publicity_normal);
        ivMsgFuncationBar.setImageResource(R.drawable.icon_home_msg_normal);
    }

    public interface OnFuncationSelectListener{
        public void OnFuncationSelect(int position);
    }

    public void setOnFuncationSelectListener(OnFuncationSelectListener onFuncationSelectListener) {
        this.onFuncationSelectListener = onFuncationSelectListener;
    }



}
